<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input style="width: 200px" v-model="params.name" placeholder="请输入姓名"></el-input>
      <el-input style="margin-left: 10px;width: 200px" v-model="params.role" placeholder="请输入权限"></el-input>
      <el-button type="info" style="margin-left: 10px" plain @click="findBySearch()">查询</el-button>
      <el-button type="warning" style="margin-left: 10px" plain @click="reset()">清空</el-button>
      <el-button type="primary" style="margin-left: 10px" plain @click="add()">新增</el-button>
      <el-upload
          action="http://localhost:8080/zcy/admin/upload"
          style="float: right"
          :show-file-list="false"
          :on-success="success_Upload">
        <el-button size="mini"v-if="user.role==='管理员'">点击上传</el-button>
      </el-upload>
    </div>
    <div>
      <el-table
          :data="tableData"
          stripe
          style="width: 100%">
        <el-table-column
            prop="name"
            label="姓名"
            width="140">
        </el-table-column>
        <el-table-column
            prop="sex"
            label="性别"
            width="100">
        </el-table-column>
        <el-table-column
            prop="password"
            label="密码"
            width="180">
        </el-table-column>
        <el-table-column
            prop="grade"
            label="年级"
            width="150">
        </el-table-column>
        <el-table-column
            prop="classes"
            label="班级"
            width="150">
        </el-table-column>
        <el-table-column
            prop="role"
            label="权限">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" size="mini" type="danger" style="margin-left: 10px">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block" style="margin-top: 15px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div>
      <el-dialog title="请输入信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form">
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <template>
              <el-radio v-model="form.sex" label="男">男</el-radio>
              <el-radio v-model="form.sex" label="女">女</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="form.password" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="年级班级" :label-width="formLabelWidth">
            <el-select v-model="form.grade" placeholder="请选择">
              <el-option label="无" value="无"></el-option>
              <el-option label="高一" value="高一"></el-option>
              <el-option label="高二" value="高二"></el-option>
              <el-option label="高三" value="高三"></el-option>
            </el-select>
            <el-input v-model="form.classes" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="权限" :label-width="formLabelWidth">
            <el-select v-model="form.role" placeholder="请选择">
              <el-option label="管理员" value="管理员"></el-option>
              <el-option label="教师" value="教师"></el-option>
              <el-option label="学生" value="学生"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  data() {
    return {
      params:{
        name:'',
        role:'',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form:{},
      formLabelWidth:"10%",
      user: localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
    }
  },
  created() {
    this.findBySearch();
  },
  methods:{
    success_Upload(res) {
      if(res.code=='0'){
        this.$message({
          message: '成功',
          type: 'success'
        })
        this.findBySearch();
      }else{
        this.$message.error({
          message: res.msg,
          type: 'error'
        })
      }
    },
    findBySearch(){
      request.get("/admin/search",{
        params: this.params
      }).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    edit(obj){
      this.form=obj;
      this.dialogFormVisible=true;
    },
    add(){
      this.form= {};
      this.dialogFormVisible=true;
    },
    reset(){
      this.params = {
        name:'',
        role:'',
        pageNum: 1,
        pageSize: 10
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    submit(){
      request.post("/admin",this.form).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: '成功',
            type: 'success'
          })
          this.dialogFormVisible=false;
          this.findBySearch();
        }else {
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    del(id){
      request.delete("/admin/"+id).then(res=>{
        if(res.code == '0'){
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.findBySearch();
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
